<?php
class OrderController extends DefaultModuleController {
	public function init() {
		parent::init();
		if(!$this->userId) {
			$this->message('请您登记录后再操作。', GODHOUSE_DOMAIN_WWW."site-login.htm");
		}
	}
	
	/* 我的订单 */
	public function actionIndex() {
		$userId = $this->userId;
		
		$status = getgpc(1);
		
		$page = intval(getgpc('page'));
		$page = max(1, $page);
		
		$sqladd = "userId='$userId'";
		if(is_numeric($status) && $status != GODHOUSE_STATUS_ORDER_CONFIRMED && $status != GODHOUSE_STATUS_ORDER_COMPLETE && $status != GODHOUSE_STATUS_ORDER_CANCEL) {
			$this->message('请选择正确的查看方式。', 'order.htm');
		} elseif (is_numeric($status)) {
			$sqladd .= " AND orderStatus='$status'";
		}
		//echo $sqladd;
		$orderList = $this->OrderModel->findAll($sqladd, $page, GODHOUSE_PPP3, 'orderId DESC');
		foreach($orderList as &$v) {
			$v['totalPrice'] = number_format($v['totalPrice'], 2, '.', '');//虽然不可能会存入三位小数点，这里是为了兼容之前的版本
			$v['status'] = $this->get_order_status($v);
			$v['createdTime'] = date('Y-m-d H:i:s', $v['createdTime']);
		}
		$num = $this->OrderModel->count($sqladd);
		$multipage = $this->page($num, GODHOUSE_PPP3, $page, 'order.htm');
		
		$this->view->assign('orderList', $orderList);
		$this->view->assign('multipage', $multipage);
		$this->display('order_list');
	}
	
	/* 订单详情 */
	public function actionDetail() {
		$orderId = getgpc(2);
		$order = $this->OrderModel->findByPk($orderId);

		// 格式化信息
		$logistic = $this->LogisticModel->findByPk($order['logisticId']);
		$order['totalPrice'] = number_format($order['totalPrice'], 2, '.', '');
		$order['logistic'] = $logistic['title'];
		$order['status'] = $this->get_order_status($order);
		$order['payTypeName'] = Lookup::item('payType', $order['payType']);
		$order['createdTime'] = date('Y-m-d H:i:s', $order['createdTime']);

		// 获取收货信息
		$consignee = $this->ConsigneeModel->find("id='".$order['consigneeId']."'");
		$consignee['cityInfo'] = $this->AreaModel->getArea($consignee['city']);
		
		// 读取操作记录
		$operationList = $this->OperationModel->findAll("module='".GODHOUSE_OPERATION_MODULE_ORDER."' AND pkId='{$orderId}'");
		foreach ($operationList as &$v) {
			$v['createdTime'] = date('Y-m-d H:i:s', $v['createdTime']);
		}

		$this->view->assign('areaList', $this->AreaModel->findAll("fId='1'", 0, 0, "id ASC"));
		$this->view->assign('order', $order);
		//$this->view->assign('totalPrice', $totalPrice);
		$this->view->assign('consignee', $consignee);
		$this->view->assign('operationList', $operationList);
		$this->view->assign('shoppingcartList', $this->_totalPrice("orderId='{$order['orderId']}'"));//这里统一调用一个方法比较好
		$this->view->assign('bottons', $bottons);
		$this->display('order_detail');
	}
	
	/* 编辑收货人信息 */
	public function actionEditConsignee() {
		$orderId = getgpc(2);
		$order = $this->OrderModel->findByPk($orderId);
		if($order['orderStatus'] == GODHOUSE_STATUS_ORDER_COMPLETE) {
			$this->message('此订单已完成交易，不能编辑收货人信息。');
		}
		$consignee = $this->ConsigneeModel->findByPk($orderId);
		
		if(isset($_POST['consignee'])) {
			$consignee = getgpc('consignee', 'P');
			$this->ConsigneeModel->updateByPk($orderId, $consignee);
			$this->message('编辑成功。', 'order-detail-'.$orderId.'.htm');
		}
		
		$this->view->assign('order', $order);
		$this->view->assign('consignee', $consignee);
		$this->display('order_edit_consignee');
	}
	
	/* 状态操作 */
	public function actionStatus() {
		$statusType 	= getgpc(2);	// 状态类型
		$orderId 	= getgpc(3);	// 订单ID	
		$status 	= getgpc(4);	// 状态值
		
		$order = $this->OrderModel->findByPk($orderId);
		if(!$order) {
			$this->message('订单不存在。', 'order.htm');
		}
		switch($statusType) {
			case 'order':
				if($order['orderStatus'] == GODHOUSE_STATUS_ORDER_PENDING) {
					if($status != GODHOUSE_STATUS_ORDER_CANCEL) {
						$this->message('请正确操作订单', 'order.htm');
					}
				}
				break;
			case 'pay':
				if($order['payStatus'] == GODHOUSE_STATUS_PAY_UNPAID) {
					if($status != GODHOUSE_STATUS_PAY_PAYMENT) {
						$this->message('请正确操作订单', 'order.htm');
					}
				}
				break;
			case 'delivery':
				if($order['deliveryStatus'] == GODHOUSE_STATUS_DELIVERY_SHIPPED) {
					if($status != GODHOUSE_STATUS_DELIVERY_RECEIVING) {
						$this->message('请正确操作订单', 'order.htm');
					}
				}
				break;
		}
		
		$att = array();
		$typeRange = array('order', 'pay', 'delivery');
		if($statusType && $orderId && is_numeric($status) && in_array($statusType, $typeRange)) {
			$att["{$statusType}Status"] = $status;
			$this->OrderModel->updateByPk($orderId, $att);
			$this->message('操作成功。', 'order.htm');
		} else {
			$this->message('出错，请联系管理员。', 'order.htm');
		}
	}
	
	/* 获取订单状态 */
	private function get_order_status($order) {
		$str = Lookup::item('orderStatus', $order['orderStatus']).',';
		if($order['orderStatus'] == GODHOUSE_STATUS_ORDER_CANCEL || $order['orderStatus'] == GODHOUSE_STATUS_ORDER_INVALID || $order['orderStatus'] == GODHOUSE_STATUS_ORDER_REFUSED) {
			$str = '<span style="color:red">'.$str.'</span>';
		}
		$str .= Lookup::item('payStatus', $order['payStatus']).',';
		$str .= Lookup::item('deliveryStatus', $order['deliveryStatus']);
		return $str;
	}
}
?>